package com.qf.controller;/*
 *徐韬
 *2021/10/25
 *20:36
 */

import com.github.pagehelper.PageInfo;
import com.qf.annotation.ignoreLog;
import com.qf.annotation.logAnnoTation;
import com.qf.dto.R;
import com.qf.pojo.MyAuthUser;
import com.qf.pojo.TAdminUser;
import com.qf.pojo.TDirtyword;
import com.qf.service.dirtywordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.PrimitiveIterator;

@RestController
@RequestMapping("/word")
@Api(tags = "敏感词管理")
public class dirtywordController {
    private dirtywordService dirtywordService;
    @Autowired
    public void setDirtywordService(com.qf.service.dirtywordService dirtywordService) {
        this.dirtywordService = dirtywordService;
    }

    @GetMapping("/selectword/{id}")
    @logAnnoTation("查询单个敏感词的数据")
    public R selectwordById(@PathVariable Long id){
        TDirtyword tDirtyword = dirtywordService.showDirtywordById(id);
        return R.setOk(tDirtyword);
    }
    @PostMapping("/add")
    public R add( String dirtyword, @ApiIgnore HttpSession session){
        TDirtyword tDirtyword = new TDirtyword();
        tDirtyword.setDirtyword(dirtyword);
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal!=null){
            MyAuthUser myAuthUser = (MyAuthUser) principal;
            Long id = myAuthUser.getId();
            Long type = myAuthUser.getType();
            tDirtyword.setCreater(id);
            tDirtyword.setOwntype(type);
            System.out.println(id);
            System.out.println(type);
        }

        dirtywordService.addDirtyword(tDirtyword);
        return R.setOk();


    }
    @GetMapping("/show")
    @ignoreLog
    public R showAll(@RequestParam(defaultValue = "1") @ApiParam(name = "pageNum",value = "页码") Integer pageNum, @RequestParam(defaultValue = "5") @ApiParam(name = "pageSize",value = "页大小") Integer pageSize){
        PageInfo<TDirtyword> pageInfo = dirtywordService.showAll(pageNum, pageSize);
        return R.setOk(pageInfo);

    }
    @PostMapping("/update")
    public R update(@RequestBody TDirtyword tDirtyword,HttpSession session){
        TAdminUser user = (TAdminUser) session.getAttribute("user");
        if(user != null){
            Long id = user.getId();
            Long type = user.getType();
            tDirtyword.setOwntype(type);
            tDirtyword.setCreater(id);
        }
        dirtywordService.updateDirtywordById(tDirtyword);
        return R.setOk();
    }
    @DeleteMapping("/delete")
    public R delete(@RequestBody Map map){
        List<Integer> ids = (List<Integer>)map.get("ids");
        dirtywordService.deleteDirtywordByids(ids);
        return R.setOk();

    }
    @GetMapping("/showByparams")
    public R showByparams(TDirtyword tDirtyword){
        List<TDirtyword> dirtywords = dirtywordService.showAllByparams(tDirtyword);
        return R.setOk(dirtywords);
    }
}
